package com.bestcem.xm.user.dao.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.user.dao.OrganizationDao;
import com.bestcem.xm.user.dao.mapper.OrganizationMapper;
import com.bestcem.xm.user.entity.mysql.Organization;
import com.bestcem.xm.user.entity.pojo.ListOrgByPageDO;
import com.bestcem.xm.user.entity.pojo.ListOrgsByFilterDO;
import com.bestcem.xm.user.entity.pojo.OrganizationDO;
import com.bestcem.xm.user.grpc.service.param.ListOrgsByPagingParam;
import com.bestcem.xm.user.util.business.DataBaseUtil;
import com.bestcem.xm.user.util.convert.OrganizationConvert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 公司管理对象层服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/1 15:00
 */
@Repository
public class OrganizationDaoImpl implements OrganizationDao {
    @Resource
    private OrganizationMapper organizationMapper;
    @Resource
    private OrganizationConvert organizationConvert;

    /**
     * 根据orgId更新Organization
     *
     * @param organizationDO
     * @return java.lang.Boolean
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/3/2 17:22
     */
    @Override
    public Boolean updateByPrimaryKeySelective(OrganizationDO organizationDO) {
        Organization organization = organizationConvert.doToEntity(organizationDO);
        DataBaseUtil.setUpdateTime(organization);
        int row = organizationMapper.updateByPrimaryKeySelective(organization);
        return row == 1;
    }

    @Override
    public Organization findById(String id) {
        return organizationMapper.findById(id);
    }

    @Override
    public Boolean updateByPrimaryKey(OrganizationDO organizationDO) {
        Organization organization = organizationConvert.doToEntity(organizationDO);
        DataBaseUtil.setUpdateTime(organization);
        int row = organizationMapper.updateByPrimaryKey(organization);
        return row == 1;
    }

    @Override
    public OrganizationDO selectByPrimaryKey(String orgId) {
        return organizationConvert.entityToDo(organizationMapper.selectByPrimaryKey(orgId));
    }

    @Override
    public OrganizationDO getOrgInfo(String orgId, Integer status) {
        return organizationConvert.entityToDo(organizationMapper.selectByOrgIdAndStatus(orgId, status));
    }

    @Override
    public OrganizationDO selectByOrgCode(String orgCode) {
        // 校验参数
        if (StringUtils.isBlank(orgCode)) {
            return null;
        }
        // 设置查询参数
        Organization organization = new Organization();
        organization.setCode(orgCode);
        // 查询并设置返回值
        List<Organization> organizations = organizationMapper.selectBySelective(organization);
        if (CollectionUtils.isEmpty(organizations)) {
            return null;
        }
        return organizationConvert.entityToDo(organizations.get(0));
    }

    @Override
    public List<OrganizationDO> selectByIdsAndStatus(List<String> orgIds, Integer status) {
        List<Organization> organizations = organizationMapper.selectByIdsAndStatus(orgIds, status);
        return organizationConvert.entitysToDos(organizations);
    }

    @Override
    public String insertSelective(OrganizationDO organizationDO) {
        // 设置参数
        Organization entity = organizationConvert.doToEntity(organizationDO);
        entity.setOrgId(StringUtils.isNotBlank(organizationDO.getOrgId()) ? organizationDO.getOrgId() : DataBaseUtil.generateId());
        DataBaseUtil.setCreateAndUpdateTime(entity);
        // 新增并设置返回值
        organizationMapper.insertSelective(entity);
        return entity.getOrgId();
    }

    @Override
    public List<String> selectOrgCodesByIds(List<String> orgIds) {
        return organizationMapper.selectOrgCodesByIds(orgIds);
    }

    @Override
    public List<OrganizationDO> selectByOrgIds(Set<String> orgIdSet) {
        return organizationConvert.entitysToDos(organizationMapper.selectByOrgIds(orgIdSet));
    }

    @Override
    public List<OrganizationDO> getExpirePackOrg(String packType, List<Integer> orgStatusList) {
        return organizationConvert.entitysToDos(organizationMapper.getExpirePackOrg(packType, orgStatusList, DataBaseUtil.getDate()));
    }

    @Override
    public List<OrganizationDO> getByfomalExpiryTime(LocalDateTime time, List<Integer> status) {
        List<Organization> organizations = organizationMapper.getByfomalExpiryTime(time, status);
        return organizationConvert.entitysToDos(organizations);
    }

    @Override
    public List<OrganizationDO> selectBySelective(OrganizationDO organizationDO) {
        List<Organization> organizations = organizationMapper.selectBySelective(organizationConvert.doToEntity(organizationDO));
        return organizationConvert.entitysToDos(organizations);
    }

    @Override
    public Integer updatePackGteTodayOrg(Integer inactivated, Integer expired, Date expireTime, Date yesterday) {
        Date now = DataBaseUtil.getDate();
        return organizationMapper.updatePackGteTodayOrg(inactivated, expired, expireTime, yesterday, now);
    }

    @Override
    public Integer updatePackLteTodayOrg(Integer inactivated, Integer expired, Date expireTime) {
        Date now = DataBaseUtil.getDate();
        return organizationMapper.updatePackLteTodayOrg(inactivated, expired, expireTime, now);
    }

    @Override
    public Integer updateCompleteFlag(Integer complete, Integer noComplete) {
        Date now = DataBaseUtil.getDate();
        return organizationMapper.updateCompleteFlag(complete, noComplete, now);
    }

    @Override
    public List<OrganizationDO> getAllOrg() {
        return (organizationConvert.entitysToDos(organizationMapper.getAllOrg()));
    }

    @Override
    public List<OrganizationDO> listOrganizationsByPage(ListOrgByPageDO pageDO, OrganizationDO org) {
        if (Objects.isNull(org)) {
            org = new OrganizationDO();
        }
        return organizationConvert.entitysToDos(organizationMapper.listOrganizationsByPage(pageDO, org));
    }

    @Override
    public Integer listOrganizationsByPageCount(ListOrgByPageDO pageDO, OrganizationDO org) {
        if (Objects.isNull(org)) {
            org = new OrganizationDO();
        }
        return organizationMapper.listOrganizationsByPageCount(pageDO, org);
    }

    @Override
    public List<String> selectOrgIdsByPackageIds(List<String> packageIdsList) {
        return organizationMapper.selectOrgIdsByPackageIds(packageIdsList);
    }

    @Override
    public List<OrganizationDO> listOrganizations(OrganizationDO organizationDO, List<Integer> statuses, String search, String tableFields, Integer limit) {
        return organizationConvert.entitysToDos(organizationMapper.listOrganizations(organizationDO, statuses, search, tableFields, limit));
    }

    @Override
    public List<OrganizationDO> listOrgsByFilter(ListOrgsByFilterDO pageDO) {
        return organizationConvert.entitysToDos(organizationMapper.listOrgsByFilter(pageDO));
    }

    @Override
    public Long listOrgsByFilterCount(ListOrgsByFilterDO pageDO) {
        return organizationMapper.listOrgsByFilterCount(pageDO);
    }

    @Override
    public List<OrganizationDO> listByOrgCodes(List<String> orgCodes) {
        return organizationConvert.entitysToDos(organizationMapper.listByOrgCodes(orgCodes));
    }

    @Override
    public List<OrganizationDO> selectByInnerFlagIsNull() {
        List<Organization> organizations = organizationMapper.selectByInnerFlagIsNull();
        return organizationConvert.entitysToDos(organizations);
    }

    @Override
    public void updateInnerFlagIsNullToFalse() {
        organizationMapper.updateInnerFlagIsNullToFalse(DataBaseUtil.getDate());
    }

    @Override
    public void updateInnerFlagByOrgIds(List<String> orgIds, Integer innerFlag) {
        organizationMapper.updateInnerFlagByOrgIds(orgIds, innerFlag, DataBaseUtil.getDate());
    }

    @Override
    public int updateCorpWalletByOrgId(String orgId, String walletId) {
        if (!ObjectId.isValid(orgId)) {
            return 0;
        }
        return organizationMapper.updateCorpWalletByOrgId(orgId, walletId);
    }

    @Override
    public int countByCode(String code) {
        return organizationMapper.countByCode(code);
    }

    @Override
    public Integer baseCount(OrganizationDO organizationDO, ListOrgsByPagingParam param, Integer limit, Integer offset) {
        return organizationMapper.baseCount(organizationDO, param, limit, offset);
    }

    @Override
    public List<OrganizationDO> listOrgsByPaging(OrganizationDO organizationDO, ListOrgsByPagingParam param, Integer limit, Integer offset) {
        return organizationConvert.entitysToDos(organizationMapper.listOrgsByPaging(organizationDO, param, limit, offset));
    }

    @Override
    public List<String> listAllIds() {
        return organizationMapper.listAllIds();
    }
}
